import {FC, useState} from "react"
import {Stats} from "fs";
interface Props {
    mobile:string
}
interface State {}
const Index:FC<Props> = ({mobile})=>{
    const [sms,setSms] = useState({
        msg:'验证码',
        t:0,
        time:10,
        disable:false,
    })

    const onSms = ()=>{

        if(sms.disable){
            return
        }
        if(!/^1\d{10}$/.test(mobile)){
            alert("手机号格式不对")
            return;
        }
        const time = sms.time - 1

        sms.time = time
        sms.msg = `剩余${sms.time - 1}s`
        sms.disable = true

        setSms({...sms})

        const t = setInterval(()=>{
            if (sms.time <= 1){
                clearInterval(t)

                sms.time = 10
                sms.msg = `验证码`
                sms.disable = false
                setSms({...sms})
                return
            }

            sms.time = sms.time - 1
            sms.msg = `剩余${sms.time - 1}s`
            setSms({...sms})
        },1000)
    }

    return <span onClick={onSms}>{sms.msg}</span>

}
export default Index
